/**********************************************************************************************************
Update [1/16]: Updated the code to fix the issue of Too Many SOQL queries in Deployment. - Arun Ramachandran
Updated [1/25/2012] : by Mike Shu - move DML and one FOR loop out of another FOR loop because of Too many script statements
**********************************************************************************************************/


trigger updateDescripLead on Event (before insert) 
 {
    ListViewIds__c LV = ListViewIds__c.getInstance();   

    ID localAdvertiserRecordTypeID = RecordTypeHelper.GetRecordTypeID('Event', 'Local Advertiser Event');   
    Id SfaNJQueueId = LV.SFA_NJ_Queue_Group__c;       
    Id SfaSLCQueueId = LV.SFA_SLC_Queue_Group__c;
    string flag;
       
    List<Lead> lstLead=new List<Lead>();
    List<Account> accList=new List<Account>();
    List<Notes__c> listObj = new List<Notes__c>();
    Set<Id> setEventIds=new Set<Id>();
    Set<Id> setAccountIds = new Set<Id>();
    
      for(Event e: Trigger.New){     
      	if(e.whoid !=null){
      		//system.debug('Lead Whoid' +e.whoid);
            string strOwnerId=(string)e.WhoId;
            if(strOwnerId.startsWith('00Q') && e.recordtypeid ==localAdvertiserRecordTypeID ){
                setEventIds.add(e.WhoId);
                //system.debug('Lead serEventIds ' +setEventIds);
            }
        }else if(e.whatid != null){
            //system.debug('Account what id' +e.whatid );
            string strOwnerAccId = (string)e.whatId;
            if(strOwnerAccId.startsWith('001') && e.recordtypeid ==localAdvertiserRecordTypeID ){
            	//system.debug('In the account whoid loop');
                setAccountIds.add(e.WhatId);
                //system.debug('Account setAccountIds ' +setAccountIds );
            }
        }
      }
      //Removed below two lines of code and updated them with If condition below: Have pasted the same two lines of code to help track the changes I made : Arun Ramachandran        
      //lstLead=[select Id, PostalCode, ownerid,name,street,phone,state,city,country, company from lead where Id in :setEventIds];
      //accList = [Select Id,Name,Phone,City__c,Zip__c,State__c,Business_Address__c from account where Id in :setAccountIds];      
        
      
     //Update made with if condition to fix deployment error - Too many SOQL Queries  -Arun Ramachandran
      if(setEventIds.size()>0){
      	lstLead=[select Id, PostalCode, ownerid,name,street,phone,state,city,country, company from lead where Id in :setEventIds];
      }
      if(setAccountIds.size()>0){
      	accList = [Select Id,Name,Phone,City__c,Zip__c,State__c,Business_Address__c from account where Id in :setAccountIds];      
      }  
        
      for(Event evt : Trigger.New){  //Event trigger
  		for(Lead ld : lstLead){                     // for Lead loop
  			if(evt.WhoId ==ld.Id){        // opening bracket for checking the Lead id
            	//system.debug('AAAAAAA '+evt.whoid );
                	if(ld.ownerid == SfaNJQueueId || ld.ownerid==SfaSLCQueueId){// ADD YOUR CONDITION CHECK  
                		//system.debug('Inside Error');  
                        evt.addError('Owner of this Lead is Queue, Accept the lead to create any event');   
                    }else{
                    	if(evt.Description!=null){
                    		evt.Description = evt.Description+'\n'+'Name :'+ld.Name+'\n'+'Company :'+ld.Company+'\n'+'Phone :'+ld.Phone;
                    	}else{
                        	evt.Description = 'Name :'+ld.Name+'\n'+'Company :'+ld.Company+'\n'+'Phone :'+ld.Phone;  
                    	}
                     	if(ld.Street!=null){evt.Location=ld.Street;}
                     	if(ld.State!=null) {evt.Location=evt.Location+','+ld.State;}
                     	if(ld.City!=null)  {evt.Location=evt.Location+','+ld.City;}
                     	if(ld.Country!=null){evt.Location=evt.Location+','+ld.Country;}
                     	if(ld.PostalCode!=null){evt.Location=evt.Location+','+ld.PostalCode;}       
                        //system.debug('I am going to enter Notes');
             		 	/* update the lead with the notes with the description on the event */   
                     	Notes__c calln= new Notes__c();                        
                     	calln.Lead__c = evt.WhoId;
                     	calln.Notes__c = evt.Description;
                     	calln.type__c = 'Lead';           
                     	calln.Disposition__c=evt.Appointment_type__c;
                     	//system.debug('calln YYYY'+ calln);
                     	listObj.add(calln);     
            		} // Ending description condition check
             } 
         } // closing Lead Loop 
         for(account acc: accList)  {
            //system.debug('ACCCCCCCCC'+acc);
           if(evt.whatid == acc.id){
	           	if(evt.Description!=null) {                
	            	evt.Description =  evt.Description+'\n'+'Name :'+acc.Name+'\n'+'Phone :'+acc.Phone;
	                //system.debug('NAme '+acc.name);
	            }else{
	                evt.Description = 'Name :'+acc.Name+'\n'+'Phone :'+acc.Phone;
	           	}
	           	if(acc.Business_Address__c!=null){ evt.Location=acc.Business_Address__c;}
	            if(acc.City__c!=null){evt.Location=evt.Location+','+acc.City__c;    }
	            if(acc.State__c!=null){evt.Location=evt.Location+','+acc.State__c;   }
	            if(acc.Zip__c!=null){evt.Location=evt.Location+','+acc.Zip__c; }                        
                
                Notes__c calln= new Notes__c();                        
                calln.Account__c = evt.WhatId;
                calln.Notes__c = evt.Description;
                calln.type__c = 'Renewal';           
                calln.Disposition__c=evt.Appointment_type__c;
                //system.debug('CCCCCCCC'+calln);
                listObj.add(calln);
                //system.debug('LLLLLL'+listObj);                  
            } // Whatid condition check ending
          }  // closing the for account loop                  
      } // closing trigger bracket 
      if(listObj.size()>0){    
      	insert listObj;
      }         
   }